<%@page import="java.util.Iterator"%>
<%@page import="com.google.gdata.data.Person"%>
<%@page import="java.util.List"%>
<%@page import="com.google.gdata.data.youtube.VideoEntry"%>
<%@page import="com.google.gdata.data.youtube.CommentEntry"%>
<%@page import="com.google.gdata.data.youtube.CommentFeed"%>
<%@page import="com.google.gdata.data.youtube.YouTubeMediaGroup"%>
<%@page import="com.google.gdata.data.youtube.PlaylistEntry"%>
<%@page import="com.google.gdata.data.youtube.PlaylistFeed"%>
<%@page import="com.google.gdata.data.youtube.PlaylistLinkEntry"%>
<%@page import="com.google.gdata.data.youtube.PlaylistLinkFeed"%>
<%@page import="com.google.gdata.client.youtube.*"%>
<%@page import="com.mathslogica.*"%>
<%@page import="java.net.URL"%>
<%@page import="com.google.gdata.util.AuthenticationException"%>
<%@page import="com.google.gdata.util.ServiceException"%>

<table class="playlist-table" cellspacing="0" cellpadding="0">
	<tr>
		<td style="width: 200px; vertical-align: top">
			<%
				YouTubeService service = new YouTubeService(MLConstants.CLIENT_ID,
						MLConstants.APP_KEY);

				String playlistID = request.getParameter(MLConstants.PLAYLIST_ID);
				String playlistUrl = MLConstants.PLAYLIST_ENTRY_PREFIX + "/"
						+ playlistID;

				try {
					/* service.setUserCredentials("moazzam.cricket@gmail.com", "indiaisthebest12!@"); */
					PlaylistFeed playlistFeed = service.getFeed(
							new URL(playlistUrl), PlaylistFeed.class);
			%>
			<ul role="navigation" id="playlist-navigation">
				<%
					for (PlaylistEntry playlistEntry : playlistFeed.getEntries()) {
							YouTubeMediaGroup mediaGroup = playlistEntry
									.getMediaGroup();
							String videoID = mediaGroup.getVideoId();
				%>
				<li><a
					href="/lesson?<%=MLConstants.PLAYLIST_ID + "=" + playlistID + "&"
							+ MLConstants.VIDEO_ID + "=" + videoID%>"><%=playlistEntry.getTitle().getPlainText()%>
				</a></li>
				<%
					}
				%>
			</ul> <%
 	} catch (AuthenticationException e) {
 		// TODO Auto-generated catch block
 		e.printStackTrace();
 	} catch (ServiceException e) {
 		// TODO Auto-generated catch block
 		e.printStackTrace();
 	}
 %>
		</td>
		<td style="width: 800px; vertical-align: top">
			<%
				String videoID = request.getParameter(MLConstants.VIDEO_ID);
			%>
			<div>
				<object width="425" height="344">
					<param name="movie"
						value="http://www.youtube.com/v/<%=videoID%>?fs=1"></param>
					<param name="allowFullScreen" value="true"></param>
					<param name="allowScriptAccess" value="always"></param>
					<embed src="http://www.youtube.com/v/<%=videoID%>?fs=1"
						type="application/x-shockwave-flash" allowfullscreen="true"
						allowscriptaccess="always" width="425" height="344">
					</embed>
				</object>
			</div>
			<div>
				<textarea>
			</textarea>
				<button>Add Comments</button>
			</div> <!-- comments -->
			<ol>
				<%
					try {
						String videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/"
								+ videoID;
						VideoEntry videoEntry = service.getEntry(
								new URL(videoEntryUrl), VideoEntry.class);
						String commentUrl = videoEntry.getComments().getFeedLink()
								.getHref();

						CommentFeed commentFeed = service.getFeed(new URL(commentUrl),
								CommentFeed.class);
						for (CommentEntry comment : commentFeed.getEntries()) {
				%>
				<li><div><%=comment.getPlainTextContent()%></div>
					<div>
						Published at:<%=comment.getPublished()%></div>
					<div>
						<%
							List<Person> authors = comment.getAuthors();
									for (Iterator<Person> iterator = authors.iterator(); iterator
											.hasNext();) {
										Person person = iterator.next();
										String name = person.getName();
						%>
						<a href="http://www.youtube.com/user/<%=name%>"><%=name%></a>,
						<%
							}
						%>
					</div></li>
				<%
					}
					} catch (Exception e) {

					}
				%>
			</ol>
		</td>
	</tr>
</table>
